package cn.edu.njust.server;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class BoundedRandomLatencyServer extends AbstractServer {

    public static final long DEFAULT_LATENCY_BOUND = 1000;

    private long latencyBound;

    private Random random = new Random();

    public BoundedRandomLatencyServer(String id) {
        super(id);
        this.latencyBound = DEFAULT_LATENCY_BOUND;
    }

    public BoundedRandomLatencyServer(String id, long latencyBound) {
        super(id);
        this.latencyBound = latencyBound;
    }

    public void service() {
        try {
            TimeUnit.MILLISECONDS.sleep(random.nextInt((int)this.latencyBound));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
